package com.hlz.fastnote


import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager

abstract class BaseCenterDialog(context: Context?) : Dialog(context!!) {
    var mRootView :View? = null

    override fun onStart() {
        super.onStart()
        window!!.attributes.width = setWidth()
        window!!.attributes.height = setHeight()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val window = this.window
        //设置位置在屏幕底部
        window!!.setGravity(Gravity.CENTER)
        //设置弹入弹出动画
//        window.setWindowAnimations(R.style.BottomDialogAnimation)
//        window.setBackgroundDrawable(context.getDrawable(R.drawable.dialog_book_discount_bg))
        window.setBackgroundDrawableResource(setBackgroundResource())
        //设置为全屏dialog
        val params = window.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.attributes = params
        mRootView = LayoutInflater.from(context).inflate(layout,null)
        setContentView(mRootView!!)
        initView()
    }


    protected abstract val layout: Int
    protected abstract fun initView()

    /**
     * todo 动态设置高度
     */
    open fun setHeight(): Int {
        return ScreenUtils.getScreenHeight(context) / 2
    }

    open fun setWidth(): Int {
        return ScreenUtils.getScreenWidth(context)
    }

    open fun setBackgroundResource(): Int {
        return R.drawable.dialog_book_discount_bg
    }
}